//
//  MainController.m
//  敢唆嘛!
//
//  Created by 教主 on 15/11/23.
//  Copyright © 2015年 zms. All rights reserved.
//

// 主controller  下面有各个主题的controller  controller的View添加在一个scrollView上
#import "MainController.h"
#import "HomeController.h"
#import "RealityController.h"
#import "SettingController.h"
#import "Device.h"
#import "AddController.h"
#import "Define.h"
// 设置按钮状态枚举 控制展开和回收
typedef NS_ENUM(int, SettingBtnState)
{
    settingOpen,
    settingClose
};

@interface MainController () <UIScrollViewDelegate>
{
    SettingBtnState settingBtnState;    // 设置按钮状态枚举 控制展开和回收
}
@property(nonatomic, strong)HomeController *homeVc;             // 主页Controller
@property(nonatomic, strong)RealityController *realityVc;       // 真事Controller
@property(nonatomic, strong)RealityController *creationVc;       // 创作
@property(nonatomic, strong)RealityController *travelVc;       // 游记
@property(nonatomic, strong)RealityController *secretVc;       // 秘密
@property(nonatomic, strong)SettingController *settingVc;       // 设置Controller
@property(nonatomic, strong)UIScrollView *mainScrollView;       // 装载所有子控制器的滚动视图
@property(nonatomic, strong)UIImageView *typeIcon;              // 顶部类型图标
@property(nonatomic, strong)UIPageControl *page;                // 顶部滚动page
@property(nonatomic, strong)UILabel *titleLabel;                     // 顶部标题
@property(nonatomic, strong)BmobUser *bmobUser;         // 用户
@end

@implementation MainController

// 添加子控制器方法
- (void)addChildController
{
    // 添加主页界面控制器
    self.homeVc = [[HomeController alloc] init];
    [self addChildViewController:_homeVc];  // 把主页控制器添加到主控制器内
    self.homeVc.view.frame = CGRectMake(KSWidth * 0, 0, KSWidth, KSHeight - 64);
    self.homeVc.view.backgroundColor = [UIColor whiteColor];
    [self.mainScrollView addSubview:_homeVc.view];
    
    // 添加真事界面控制器
    self.realityVc = [[RealityController alloc] init];
    self.realityVc.isReality = YES;
    [self addChildViewController:_realityVc];  // 把真事控制器添加到主控制器内
    self.realityVc.view.frame = CGRectMake(KSWidth * 1, 0, KSWidth, KSHeight - 64);
    self.realityVc.view.backgroundColor = [UIColor whiteColor];
    // 暂时先不隐藏 看后期效果
    [self.mainScrollView addSubview:_realityVc.view];
    
    // 添加创作界面控制器
    self.creationVc = [[RealityController alloc] init];
    self.creationVc.isCreation = YES;
    [self addChildViewController:_creationVc];  // 把创作控制器添加到主控制器内
    self.creationVc.view.frame = CGRectMake(KSWidth * 2, 0, KSWidth, KSHeight - 64);
    self.creationVc.view.backgroundColor = [UIColor whiteColor];
    // 暂时先不隐藏 看后期效果
    [self.mainScrollView addSubview:_creationVc.view];
    
    // 添加游记界面控制器
    self.travelVc = [[RealityController alloc] init];
    self.travelVc.isTravel = YES;
    [self addChildViewController:_travelVc];
    self.travelVc.view.frame = CGRectMake(KSWidth * 3, 0, KSWidth, KSHeight - 64);
    self.travelVc.view.backgroundColor = [UIColor whiteColor];
    // 暂时先不隐藏 看后期效果
    [self.mainScrollView addSubview:_travelVc.view];
    
    // 添加秘密界面控制器
    self.secretVc = [[RealityController alloc] init];
    self.secretVc.isSecret = YES;
    [self addChildViewController:_secretVc];
    self.secretVc.view.frame = CGRectMake(KSWidth * 4, 0, KSWidth, KSHeight - 64);
    self.secretVc.view.backgroundColor = [UIColor lightGrayColor];
    // 暂时先不隐藏 看后期效果
    [self.mainScrollView addSubview:_secretVc.view];
    
    // 添加设置界面控制器
    self.settingVc = [[SettingController alloc] init];
    [self addChildViewController:_settingVc];  // 把设置控制器添加到主控制器内
    self.settingVc.view.frame = CGRectMake(-KSWidth, 0, KSWidth - KSWidth / 2, KSHeight);
    self.settingVc.view.backgroundColor = [UIColor colorWithRed:25 / 255.0 green:25 / 255.0 blue:36 / 255.0 alpha:1.0];
    // 暂时先不隐藏 看后期效果
    [self.view addSubview:_settingVc.view];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGFloat x = KSWidth / 6.5;
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSWidth / 2.5, KSWidth / 25)];
    // 顶部类型图标
    self.typeIcon = [[UIImageView alloc] initWithFrame:CGRectMake((topView.frame.size.width - KSWidth / 26) / 2.8, topView.frame.size.height - KSWidth / 17, KSWidth / 26, KSWidth / 26)];
    self.typeIcon.image = [UIImage imageNamed:@"home"];
    [topView addSubview:_typeIcon];
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.typeIcon.frame.size.width + x / 1.1 , topView.frame.size.height - KSWidth / 17, KSWidth / 8, KSWidth / 28)];
    self.titleLabel.text = @"首页";
    self.titleLabel.alpha = 0.8;
    self.titleLabel.font = [UIFont systemFontOfSize:KSWidth / 22];
    [topView addSubview:_titleLabel];
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(topView.frame.size.width / 2, self.typeIcon.frame.size.width, 2, 2)];
    self.page.currentPageIndicatorTintColor = [UIColor colorWithRed:60 / 255.0 green:60 / 255.0 blue:60 / 255.0 alpha:1.0];
    self.page.pageIndicatorTintColor = [UIColor colorWithRed:205 / 255.0 green:205 / 255.0 blue:205 / 255.0 alpha:1.0];
    self.page.numberOfPages = 5;
    self.page.currentPage = 0;
    [topView addSubview:_page];
    self.navigationItem.titleView = topView;
    
    // 添加main滚动视图
    self.mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, KSWidth, KSHeight - 64)];
    self.mainScrollView.contentSize = CGSizeMake(KSWidth * 5, KSHeight - 64);
    self.mainScrollView.contentOffset = CGPointMake(KSWidth * 0, 0);
    self.mainScrollView.pagingEnabled = YES;
    self.mainScrollView.bounces = NO;
    self.mainScrollView.delegate = self;
    self.mainScrollView.showsHorizontalScrollIndicator = NO;
    self.mainScrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_mainScrollView];
    
    // 添加子控制器方法
    [self addChildController];
    
    // 创建左上角设置按钮
    UIButton *settingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    settingBtn.frame = CGRectMake(0, 0, KSWidth / 13, KSWidth / 13);
    [settingBtn setImage:[UIImage imageNamed:@"iconfont-yonghu"] forState:UIControlStateNormal];
    [settingBtn addTarget:self action:@selector(settingBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftSettingBtn = [[UIBarButtonItem alloc] initWithCustomView:settingBtn];
    self.navigationItem.leftBarButtonItem = leftSettingBtn;
    
    // 创建左上角按钮组: 添加
    UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSWidth / 5, KSWidth / 25)];
//    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    searchBtn.frame = CGRectMake(0, KSWidth / 200, KSWidth / 20, KSWidth / 20);
//    [searchBtn setImage:[UIImage imageNamed:@"iconfont-search"] forState:UIControlStateNormal];
//    [settingBtn addTarget:self action:@selector(searchBtnAction:) forControlEvents:UIControlEventTouchUpInside];
//    [buttonView addSubview:searchBtn];
    
    UIButton *AddBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    AddBtn.frame = CGRectMake((KSWidth / 20) + KSWidth / 13, -5, KSWidth / 13, KSWidth / 13);
    [AddBtn setImage:[UIImage imageNamed:@"iconfont-icon204"] forState:UIControlStateNormal];
    [AddBtn addTarget:self action:@selector(AddBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [buttonView addSubview:AddBtn];
    
    UIBarButtonItem *rightButtonView = [[UIBarButtonItem alloc] initWithCustomView:buttonView];
    self.navigationItem.rightBarButtonItem = rightButtonView;

    // 初始化设置按钮状态
    settingBtnState = settingClose;
}

// 提供给外部禁用开启滑动
- (void)scrollOnorOff:(BOOL)onOrOff
{
    self.mainScrollView.scrollEnabled = onOrOff;
    self.navigationItem.rightBarButtonItem.enabled = onOrOff;
    self.navigationItem.leftBarButtonItem.enabled = onOrOff;
}

// 提供给外部调用 来设置scrollView的偏移量
- (void)setScrollViewOffset:(CGFloat)offX
{
    [self.mainScrollView setContentOffset:CGPointMake(offX, 0) animated:YES];
    self.page.currentPage = offX / KSWidth;
    switch (_page.currentPage) {
        case 1:
        {
            self.titleLabel.text = @"真事";
            self.typeIcon.image = [UIImage imageNamed:@"iconfont-yuanquan"];
            break;
        }
        case 2:
        {
            self.titleLabel.text = @"创作";
            self.typeIcon.image = [UIImage imageNamed:@"iconfont-yuanquan-3"];
            break;
        }
        case 3:
        {
            self.titleLabel.text = @"游记";
            self.typeIcon.image = [UIImage imageNamed:@"iconfont-yuanquan-2"];
            break;
        }
        case 4:
        {
            self.titleLabel.text = @"秘密";
            self.typeIcon.image = [UIImage imageNamed:@"iconfont-yuanquan-1"];
            break;
        }

    }

}

//// 搜索按钮触发
//- (void)searchBtnAction:(UIButton *)searchBtn
//{
//    
//}

// 添加按钮触发
- (void)AddBtnAction:(UIButton *)AddBtn
{
    // 判断当前用户是否登录中
    self.bmobUser = [BmobUser getCurrentUser];
    if (_bmobUser)
    {
        AddController *addVc = [[AddController alloc] init];
        addVc.userName = self.bmobUser.username;
        
        BmobQuery *bquery = [BmobQuery queryWithClassName:@"_User"];
        bquery.limit = 1000;
        //查找Recommend表的数据
        [bquery getObjectInBackgroundWithId:_bmobUser.objectId block:^(BmobObject *object, NSError *error)
         {
            if (error){
                //进行错误处理
                NSLog(@"%@",error);
            }
            else
            {
                if (object)
                {
                   // 取到数据库中头像文件
                    BmobFile *userHeaderFile = [object objectForKey:@"userHeader"];
                    // 用第三方转化下
                    [addVc.headerImageView sd_setImageWithURL:[NSURL URLWithString:userHeaderFile.url]];
                }
            }
         }];
        [self.navigationController pushViewController:addVc animated:YES];
    }
    else
    {
        [self promptViewWithTitle:@"请先登录哟~"];
    }

}

// 左上角设置按钮
- (void)settingBtnAction:(UIButton *)settingBtn
{
    if (settingBtnState == settingClose)
    {
        // 展开动画
        [UIView animateWithDuration:0.4 animations:^{
            self.settingVc.view.frame = CGRectMake(0, 0, KSWidth - KSWidth / 2, KSHeight);
        }];
        settingBtnState = settingOpen;
    }
    else
    {
        // 回收动画
        [UIView animateWithDuration:0.4 animations:^{
            self.settingVc.view.frame = CGRectMake(-KSWidth, 0, KSWidth - KSWidth / 2, KSHeight);
        }];
        settingBtnState = settingClose;
    }
}

// scrollView代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.page.currentPage = scrollView.contentOffset.x / KSWidth;
    switch (self.page.currentPage) {
        case 0:
        {
            self.titleLabel.text = @"首页";
            self.typeIcon.image = [UIImage imageNamed:@"home"];
            break;
        }
        case 1:
        {
            self.titleLabel.text = @"真事";
            self.typeIcon.image = [UIImage imageNamed:@"iconfont-yuanquan"];
            break;
        }
        case 2:
        {
            self.titleLabel.text = @"创作";
            self.typeIcon.image = [UIImage imageNamed:@"iconfont-yuanquan-3"];
            break;
        }
        case 3:
        {
            self.titleLabel.text = @"游记";
            self.typeIcon.image = [UIImage imageNamed:@"iconfont-yuanquan-2"];
            break;
        }
        case 4:
        {
            self.titleLabel.text = @"秘密";
            self.typeIcon.image = [UIImage imageNamed:@"iconfont-yuanquan-1"];
            break;
        }
    }
}


// 弹出提示窗口
- (void)promptViewWithTitle:(NSString *)title
{
    UIView *promptView = [[UIView alloc] initWithFrame:CGRectMake(-(KSWidth - (KSWidth / 2)) / 2, KSWidth - KSWidth / 4, KSWidth / 2, KSWidth / 4)];
    promptView.backgroundColor = [UIColor whiteColor];
    promptView.alpha = 0;
    promptView.layer.cornerRadius = 10;
    promptView.layer.borderColor = [UIColor colorWithRed:150 / 255.0 green:150 / 255.0 blue:150 / 255.0 alpha:1.0].CGColor;
    promptView.layer.borderWidth = 1;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(-(KSWidth - (KSWidth / 2)) / 2, KSWidth - KSWidth / 4, KSWidth / 2, KSWidth / 4)];
    titleLabel.text = title;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.font = [UIFont systemFontOfSize:KSWidth / 25];
    titleLabel.alpha = 0;
    [self.view addSubview:promptView];
    [self.view addSubview:titleLabel];
    
    [UIView animateWithDuration:0.4 animations:^{
        promptView.alpha = 0.8;
        titleLabel.alpha = 0.8;
        promptView.frame = CGRectMake((KSWidth - (KSWidth / 2)) / 2, KSWidth - KSWidth / 4, KSWidth / 2, KSWidth / 4);
        titleLabel.frame = CGRectMake((KSWidth - (KSWidth / 2)) / 2, KSWidth - KSWidth / 4, KSWidth / 2, KSWidth / 4);
    }completion:^(BOOL finished) {
        // 延迟一秒
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:0.4 animations:^{
                promptView.alpha = 0;
                titleLabel.alpha = 0;
                promptView.frame = CGRectMake(((KSWidth - (KSWidth / 2)) / 2) * 2, KSWidth - KSWidth / 4, KSWidth / 2, KSWidth / 4);
                titleLabel.frame = CGRectMake(((KSWidth - (KSWidth / 2)) / 2) * 2, KSWidth - KSWidth / 4, KSWidth / 2, KSWidth / 4);
            }completion:nil];
        });
    }];
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
